package com.twu.biblioteca;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;

/**
 * Created by mauricio on 9/25/14.
 */
public class Menu {
    private boolean done = false;
    private ArrayList<HashMap<String, String>> options;
    private String title;


    public Menu(String title) {
        this.title = title;
        options = new ArrayList<HashMap<String, String>>();

    }
    public Menu() {
        this.title = "Menu";
    }

    public ArrayList<HashMap<String, String>> getOptions() {
        return this.options;
    }



    public void add(String name, String controller, String action) {
        HashMap option = new HashMap();
        option.put("name", name);
        option.put("controller", controller);
        option.put("action", action);
        this.options.add(option);
    }

    private void print() {

        System.out.println("\n" + title + "\n");
        for(int key = 0; key < options.size(); key++) {
            System.out.println("..[" + key + "] " + options.get(key).get("name"));
        }
        System.out.println();
    }



    private void execute(String option) {
            if(option.equals("q")) {
                this.done = true;
            } else {

                if (isNumeric(option)) {
                    if (Integer.parseInt(option) < options.size() && Integer.parseInt(option) >= 0) {
                        try {
                            HashMap hashMap = this.options.get(Integer.parseInt(option));

                            String controllerName = "com.twu.biblioteca." + hashMap.get("controller") + "Controller";
                            String actionName = hashMap.get("action") + "Action";
                            Class controller = Class.forName(controllerName);
                            Method action = controller.getDeclaredMethod(actionName);
                            action.invoke(controller.newInstance());
                        } catch (NoSuchMethodException e) {
                            e.printStackTrace();
                        } catch (InvocationTargetException e) {
                            e.printStackTrace();
                        } catch (InstantiationException e) {
                            e.printStackTrace();
                        } catch (IllegalAccessException e) {
                            e.printStackTrace();
                        } catch (ClassNotFoundException e) {
                            e.printStackTrace();
                        }
                    } else {
                        System.out.println("Select a valid option!");
                    }
                } else {
                    System.out.println("Select a valid option!");
                }
            }


    }

    private boolean isNumeric(Object str) {
        try {
            int i = Integer.parseInt(str.toString());
        }
        catch(NumberFormatException nfe) {
            return false;
        }
        return true;
    }

    public void loop() {
        String separatorLine = "-----------------------------------------------------";
        while(!done) {
            this.print();
            System.out.print("Enter option or type q to quit: ");
            String option = new Scanner(System.in).next();
            System.out.println(separatorLine);
            this.execute(option);
            System.out.println(separatorLine);
        }
    }


}
